package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.CommentqueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.TimeUtil;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StrategyDoubleCommentMapper strategyDoubleCommentMapper;
    @Autowired
    private TravelCommentMapper travelCommentMapper;
    public void save(User user) {
        User temp = userMapper.selectByEmailAndPassword(user.getEmail(),null);
        if (temp==null) {
            user.setHeadImgUrl("img/user/head.jpg");
            user.setCoverImgUrl("img/user/bg.jpeg");
            user.setState(User.STATE_ENABLE);
            userMapper.insert(user);

        }else {
            throw new RuntimeException("邮箱已经被注册了!");
        }
    }

    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if (user==null){
            throw new RuntimeException("账号或者密码错误");
        }
        return user;
    }

    public PageInfo<User> listAll(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo<>(userMapper.selectAll(qo));
    }

    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    public User getUser(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public boolean isFollower(Long currentId, Long userId) {
        return userMapper.selectRelation(currentId,userId);
    }

    public void saveRelation(Long currentId, Long userId) {
        userMapper.insertRelation(currentId,userId);
    }

    public void deleteRelation(Long currentId, Long userId) {
        userMapper.deleteRelation(currentId,userId);
    }

    public void updateFansAndFollowersCount(User user) {
        userMapper.updateFansAndFollowersCount(user);
    }

    @Override
    public void updateState(Long userId,long stateDisable) {
        userMapper.updateState(userId,stateDisable);
    }

    @Override
    public User selectUserById(Long id) {
        return  userMapper.selectByPrimaryKey(id);
    }


    public Object countUnReadcomment(CommentqueryObject qo) {
        //判断是否 查出 为null
        Integer countUnReadcomment = travelCommentMapper.countUnReadcomment(qo);
        if(countUnReadcomment == null){
            return 0;
        }else {
            return  countUnReadcomment;
        }
    }

    public PageInfo<?> usecomment(CommentqueryObject qo) {
        qo.setPageSize(6);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"c.commentTime");
        List<Map> list = travelCommentMapper.usecomment(qo);
        for (Map map : list) {
            Date createTime = (Date)map.get("commentTime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("commentTime",Object);
            //处理 回显信息
            Object title = map.get("title");
            Object authorNickName = map.get("authorNickName");
            String message = "你评价\""+authorNickName +"\"的游记";
            map.put("message",message);
        }
        return new PageInfo<>(list);
    }

    public PageInfo<?> othercomment(CommentqueryObject qo) {
      /* PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<?> strategyDoubleComments = strategyDoubleCommentMapper.countUnReadcomment(qo);*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"c.commentTime");
        List<Map> travelComments = travelCommentMapper.othercomment(qo);
        for (Map map : travelComments) {
            Date createTime = (Date)map.get("commentTime");
            //格式化
            String Object = TimeUtil.DateformatTime(createTime);
            map.put("commentTime",Object);
            //处理 回显信息
            Object title = map.get("title");
            Object travelId = map.get("travelId");
            String message = "评价你的游记\""+title+"\"";
            map.put("message",message);
        }
        return new PageInfo(travelComments);
    }
}
